package com.bs.manager04.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bs.manager04.entity.Menu;
import com.bs.manager04.entity.Role;
import com.bs.manager04.service.IMenuService;
import com.bs.manager04.service.IRoleService;
import com.bs.manager04.service.IRolemenuService;
import com.bs.manager04.utils.Result;
import com.bs.manager04.utils.auth.Auth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 角色管理表 前端控制器
 * </p>
 *
 * @author yao
 * @since 2022-12-17
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理表")
public class RoleController {
    @Resource
    IRoleService service;

    @Resource
    IRolemenuService service1;


    @GetMapping
    @ApiOperation("角色分页条件查询")
    @Auth("system:role")
    public Result getIndexMenu(@ApiParam("当前页") @RequestParam(defaultValue = "1") Integer pageNum, @ApiParam("页面大小") @RequestParam(defaultValue = "5") Integer pageSize, @ApiParam("角色名")  @RequestParam(defaultValue = "") String name){
        Page<Role> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Role> lq=new LambdaQueryWrapper<>();
        lq.like(StringUtils.isNotBlank(name),Role::getRolename,name);
        page=service.page(page,lq);
        return Result.success(page);
    }


    @PostMapping
    @ApiOperation("角色添加")
    @Auth("system:role:add")
    public Result addMenu(@ApiParam("角色表")@RequestBody Role menu){
        boolean save = service.save(menu);
        if(save){
            return Result.success("添加角色信息成功");
        }else{
            return Result.fail("添加角色信息失败");
        }
    }


    @GetMapping("/{id}")
    @ApiOperation("根据角色id查询角色列表")
    @Auth("system:role")
    public Result getByMenuID(@PathVariable("id") @ApiParam("角色id") String id){
        Role menu = service.getById(id);
        return Result.success(menu);
    }


    @PutMapping
    @ApiOperation("角色修改")
    @Auth("system:role:update")
    public Result updateMenu(@RequestBody @ApiParam("角色表") Role menu){
        boolean save = service.updateById(menu);
        if(save){
            return Result.success("修改角色信息成功");
        }else{
            return Result.fail("修改角色信息失败");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("角色删除")
    @Auth("system:role:del")
    public Result delMenu(@PathVariable("id") @ApiParam("角色id") String id){
        boolean del = service.removeById(id);
        if(del){
            return Result.success("删除角色信息成功");
        }else{
            return Result.fail("删除角色信息失败");
        }
    }

    @PutMapping("/addMenus")
    @ApiOperation("给角色分配对应的菜单权限")
    public Result addMenus(@ApiParam("角色id") @RequestParam String roleid,@ApiParam("菜单id") @RequestParam String[] menuids){
        int row = service1.addMenus(roleid, menuids);
        if(row>0){
            return Result.success("成功");
        }else{
            return Result.fail("失败");
        }
    }
}

